//agentStore.reload();
var userInfoFormConfig={
		xytpe:'form',
        width: 600,
        padding: '0 20px 0 20px',
        xtype: 'form',
        id: 'addUserForm',
        frame: true,
		autoDestroy:false,
		layout:'column',
        defaults: {
        	layout:'form',
			columnWidth:0.5,
			labelWidth: 70,
			width:300,
			defaults: {
				anchor: "95%",
				xtype: "textfield",
				invalidText:'该输入项不能为空!',
				allowBlank: false
			}
        },
        items: [{
			items:[{
	            fieldLabel: "用&nbsp;户&nbsp;名&nbsp;称",
				name:'userName',
				invalidText:'验证失败',
				emptyText : '请填写用户名',
				validator:usernameValidator,
				validationEvent:'blur',
				id:'userName_fl'
	        },{
	            fieldLabel: "用户ID",
				xtype:'hidden',
				name:'userId',
//				allowBlank : true,
				id:'userId_fl'
	        },{
				xtype:'combo',
	            fieldLabel: "用&nbsp;户&nbsp;类&nbsp;型",
	            emptyText:'请选择用户类型',
				mode:'local',
	            triggerAction : 'all',
	            editable : false,
	            displayField : 'text',
	            valueField : 'value',
	            store : purviewStore,
				name:'userType',
				id:'userType_fl',
				listeners:{
					select:function(combo,record,index ){
						var form = combo.findParentByType('form');
						var value=record.get('value');
						changeUserType('addUserForm',value);
					}
				}
	        },{
	            fieldLabel: "所属总代理",
				xtype:'combo',
				name:'parentAgentId',
				triggerAction : 'all',
				emptyText :'请选择总代理',
				lazyInit :true,
				allowBlank: true,
	            displayField : 'userName',
	            valueField : 'userId',
				store:agentStore,
				id:'parentAgentId_fl'
	        }, {
				xtype:'combo',
	            fieldLabel: "所属经销商",
				emptyText :'请选择经销商',
				triggerAction : 'all',
	            displayField : 'userName',
	            valueField : 'userId',
				store:DealerStore,
				name:'parentDealerId',
				id:'parentDealerId_fl',
				listeners: {
					select: function(combo, record, index){
						var userType = Ext.getCmp('userType_fl');
						if (userType.getValue() == "D") {
							var form = combo.findParentByType('form');
							var escape = ['userName_fl', 'userId_fl', 'userType_fl', 'parentDealerId_fl', 'parentAgentId_fl']
							Ext.Ajax.requestSyn({
								url: "findUserByUserId.action",
								params: {
									json: record.get('userId')
								},
								success: function(rep){
									var msg = Ext.decode(rep);
									if (msg.success) {
										panel_initData(form, escape, msg.obj);
									}
								}
							});
						}
					}
				}
	       },{
	            fieldLabel: "公&nbsp;司&nbsp;名&nbsp;称",
				name:'cmpName',
				emptyText :'请填写公司名称',
				id:'cmpName_fl'
	        }, {
	            fieldLabel: "品&nbsp;牌&nbsp;名&nbsp;称",
				emptyText :'请填写品牌名称',
				name:'brandName',
				id:'brandName_fl'
	        }, {
	            fieldLabel: "联&nbsp;&nbsp;&nbsp;系&nbsp;&nbsp;&nbsp;人",
				emptyText :'请填写联系人名称',
				name:'contact',
				id:'contact_fl'
	        }, {
	            fieldLabel: "联系人电话",
				emptyText :'请填写联系人电话',
				name:'contactTel',
				id:'contactTel_fl'
	        } ]
		},{
			items:[{
	            fieldLabel: "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;E-mail",
				name:'email',
				emptyText :'请填写电子邮箱地址',
				id:'email_fl',
	            allowBlank: true
	        }, {
	            fieldLabel: "公&nbsp;司&nbsp;电&nbsp;话",
				emptyText :'请填写公司电话',
				name:'cmpTel',
				id:'cmpTel_fl'
	        }, {
	            fieldLabel: "传&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;真",
				emptyText :'请填写传真',
				name:'cmpFax',
				id:'cmpFax_fl'
	        }, {
	            fieldLabel: "收&nbsp;&nbsp;&nbsp;件&nbsp;&nbsp;&nbsp;人",
				emptyText :'请填写收件人',
				name:'addressee',
				id:'addressee_fl'
	        }, {
	            fieldLabel: "收件人电话",
				emptyText :'请填写收件人电话',
				name:'addresseeTel',
				id:'addresseeTel_fl'
	        }, {
	            fieldLabel: "所&nbsp;属&nbsp;区&nbsp;域",
				emptyText :'请填写所属区域',
				name:'area',
				id:'area_fl',
	            allowBlank: true
	        }, {
	            fieldLabel: "营&nbsp;业&nbsp;执&nbsp;照",
				emptyText :'请填写营业执照',
				name:'license',
				id:'license_fl',
	            allowBlank: true
	        }, {
	            fieldLabel: "税&nbsp;务&nbsp;登&nbsp;记",
				emptyText :'请填写税务登记编号',
				name:'taxRegistration',
				id:'taxRegistration_fl',
	            allowBlank: true
        	}, {
//	            fieldLabel: "公司标志",
				name:'logo',
				id:'logo_fl',
				allowBlank : true,
	            hidden : true
        	}, {
//	            fieldLabel: "页头大图",
				name:'bigPic',
				id:'bigPic_fl',
				allowBlank : true,
	            hidden: true
        	}, {
//	            fieldLabel: "页头小图",
				name:'smallPic',
				id:'smallPic_fl',
				allowBlank : true,
	            hidden: true
        	}]
        },{
        	  columnWidth:1,
			  defaults:{
			  	anchor:'97.5%',
				xtype:'textfield'
			  },
			items:[{
	            fieldLabel: "公&nbsp;司&nbsp;地&nbsp;址",
				emptyText :'请填写公司地址',
				name:'cmpAddress',
				id:'cmpAddress_fl'
				
	        }, {
	            fieldLabel: "邮&nbsp;寄&nbsp;地&nbsp;址",
				emptyText :'请填写邮寄地址',
				name:'postAddress',
				id:'postAddress_fl'
	        }, {
	            fieldLabel: "备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注",
				height:70,
				name:'memo',
				id:'memo_fl',
	            allowBlank: true
	        }]
	     }]
    }

var addUserWindowConfig={
    title: "添加用户",
	modal :true,
	id:'addUserWindow',
	autoDestroy:false,
	resizable :false,
	width:600,
    closable: false,
    buttons: [{
        text: "提交",
        handler: SaveUserInfo
    }, {
        text: "重置",
		handler:function(bt,e){
			var parent = bt.ownerCt.ownerCt;
			var form=parent.findBy(function(cmp,pa){
				if(cmp.xytpe=='form')
					return true;
				return false;
			});
			if (Ext.isArray(form)) {
				Ext.each(form,function(f){
					f.getForm().reset();
				})
				
			}
		}
    }, {
        text: "返回",
        handler: function(bt, e){
            var parent = bt.ownerCt.ownerCt;
            parent.destroy();
        }
    }],
		 listeners:{
		 	show:function show(win){
				var form=win.findByType('form');
				var userType=Ext.getCmp('userType_fl');
				changeUserType(form,userType.getValue())
			}
		 }
}

var updateUserWindowConfig={
    title: "修改用户",
	width:600,
	modal :true,
	id:'addUserWindow',
	autoDestroy:false,
	resizable :false,
    closable: false,
    buttons: [{
        text: "修改",
        handler: SaveUserInfo
    }, {
        text: "关闭",
        handler: function(bt, e){
            var parent = bt.ownerCt.ownerCt;
			var form=parent.findByType('form')
			if(Ext.isObject(form))
				form.getForm().reset();
            parent.destroy();
        }
    }],
	 listeners:{
		 	show:function show(win){
				var form=win.findByType('form');
				var userType=Ext.getCmp('userType_fl');
				changeUserType(form,userType.getValue())
			}
		 }
}
//
function SaveUserInfo(bt, e){
            var parent = bt.ownerCt.ownerCt;
			var form=parent.findByType('form')
			var tag = form[0].getForm().isValid();
			if (tag) {
				var data=panel_gatherData(form[0]);
				parent.destroy();
				var con=new connection({
					url:'addOrUpdateUser.action',
					params:{
						json:Ext.encode(data)
					},
					success:function(rep,opt){
						var alt=rep.success?'成功':'失败';
						Ext.Msg.alert(alt, rep.msg);
						searchUserInfo();
					}
				})
				con.submit();
			}else{
				Ext.Msg.alert('提示', '信息不完整，请填写完整!');
			}
        }

/**查询用户信息; */
function searchUserInfo(data){
	var store=userGrid.getStore();
	Ext.apply(store.baseParams, {
		json:Ext.encode(data)
	})
	store.load({});
}
/** 检查用户名 */
function usernameValidator(value){
	value=value.trim();
	if(value==null||value=="")return false;
    var rslt=true;
    Ext.Ajax.requestSyn({
		url: "checkUsername.action",
		params: { json: value },
		success: function(rep){
			var msg = Ext.decode(rep);
			if(!msg.success){
				Ext.Msg.alert("提示","该用户名已存在,请重新输入");
				rslt=false;
			}
        }
    });
    return rslt;
}

/**切换用户角色的按钮响应事件 **/
function changeUserType(form,value){
	var escapes=['userName_fl','userType_fl']
		if (value == 'B') {
			panel_disabled(form, ['parentAgentId_fl', 'parentDealerId_fl'],escapes,false);
		}
		else if (value == 'C') {
			panel_disabled(form,'parentDealerId_fl', escapes,false);
		}
		else if (value == 'D') {
			panel_disabled(form, 'parentDealerId_fl',escapes);
		}
		else{
			panel_disabled(form,'userType_fl',escapes);
		}
}
